<%@ page import="pe.com.mowa.siscobsms.datos.*" %>
<%@ page import="pe.com.mowa.siscobsms.bean.persona" %>
<%@ page import="pe.com.mowa.siscobsms.bean.analista" %>
<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script>
//busca caracteres que no sean espacio en blanco en una cadena  

function redondear(cantidad, decimales) {
var cantidad = parseFloat(cantidad);
var decimales = parseFloat(decimales);
decimales = (!decimales ? 2 : decimales);
return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
} 

function calculaUtilidad()
{
var mentre=document.getElementById("mnentre").value;
var numcuotas=document.getElementById("cuotas").value;
var interes=document.getElementById("interes").value;
var cuota = (mentre/numcuotas)+((mentre*interes)/(100*numcuotas));
document.getElementById("mncuota").value=redondear(cuota,2);
cuota=document.getElementById("mncuota").value;
 
var total =(cuota*numcuotas);

document.getElementById("utilidad").value=total-mentre;

}


function calculaCuotas()
{
var mentre=document.getElementById("mnentre").value;
var numcuotas=document.getElementById("cuotas").value;
var interes=document.getElementById("interes").value;
var cuota = (mentre/numcuotas)+((mentre*interes)/(100*numcuotas));
document.getElementById("mncuota").value=redondear(cuota,2);
} 


function calculaTotal()
{
var mentre=document.getElementById("mnentre").value;
var numcuotas=document.getElementById("cuotas").value;
var interes=document.getElementById("interes").value;
var cuota = (mentre/numcuotas)+((mentre*interes)/(100*numcuotas));
document.getElementById("mncuota").value=redondear(cuota,2);
cuota=document.getElementById("mncuota").value;
 
document.getElementById("mntotal").value=(cuota*numcuotas);

} 



function vacio(q) {  
        for ( i = 0; i < q.length; i++ ) {  
                if ( q.charAt(i) != " " ) {  
                        return true  ;
                }  
        }  
        return false  
}  

function validaMnEntre(F) {  
          
        if(vacio(F.mnentre.value) == false ) {  
                return false;  
        } else {  
                return true ; 
        }  
          
}  

function validaCuotas(F) {  
          
        if(vacio(F.cuotas.value) == false ) {  
                return false;  
        } else {  
                return true ; 
        }  
          
}  

function limpiar()
{
document.getElementById("idpersona").value='-1';
document.getElementById("idlocal").value='-1';
document.getElementById("cuotas").value='';
document.getElementById("mnentre").value='';
document.getElementById("interes").value='';
document.getElementById("mncuota").value='';
document.getElementById("mntotal").value='';
document.getElementById("utilidad").value='';
document.getElementById("comentario").value='';


}

function setPago()
{
    document.getElementById("frmRegPrestamo").action="setPago";
	document.getElementById("frmRegPrestamo").submit();
}

function setPrestamo()
{
    if (document.getElementById("idpersona").value=='-1')
    {
    alert('Debe seleccionar una persona');
    return;
    }

    if (document.getElementById("idlocal").value=='-1')
    {
    alert('Debe seleccionar un local');
    return; 
    }
    
    if (!validaMnEntre(document.getElementById("frmRegPrestamo")))
    {
    alert('Debe ingresar un monto');
    return; 
    }

    if (!validaCuotas(document.getElementById("frmRegPrestamo")))
    {
	document.getElementById("frmRegPrestamo").cuotas.value="30";
    }

	  
    if (document.getElementById("btnGuardar").value=='Grabar')
    document.getElementById("frmRegPrestamo").action="setPrestamo";
    else
    if (document.getElementById("btnGuardar").value=='Actualizar')
    document.getElementById("frmRegPrestamo").action="updPrestamo";
  
	
	
	document.getElementById("frmRegPrestamo").submit();
	
}
	

function repPrestamos(idpersona)
{
 	document.getElementById("frRepPrestamos").src="repPrestamos.jsp?idpersona="+idpersona;
  
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<table width="1000" border="0">
  <tr>
    <td>
	<form mane="frmRegPrestamo" id="frmRegPrestamo" method="post">
	<table width="100%" border="0" >
      <tr>
        <td colspan="10" bgcolor="#BEDFEF">P&eacute;stamo</td>
        </tr>
      <tr>
        <td></td>
        <td colspan="3">&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td width="1%">        </td>
        <td colspan="3"><input type="hidden" name="idprestamo"  id="idprestamo" /></td>
        <td width="35%">Comentario</td>

        <td width="1%">&nbsp;</td>
        <td width="21%">&nbsp;</td>
        <td width="16%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
        <td width="1%">&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td width="8%">Persona</td>
        <td colspan="2" nowrap="nowrap"><select name="idpersona" id="idpersona" onchange="repPrestamos(this.value)">
		<%
			conPersona cp = new conPersona();
				ArrayList personas= cp.getPersonas();
				String nomPer;
				String idPer;
				persona per;
			out.print("<option value='-1'> -- </option>");
				
		     		for (int i = 0; i < personas.size(); i++) {
		     		per=(persona) personas.get(i); 
		     		nomPer=per.getNombre()+" "+per.getApePat()+" "+per.getApeMat();
		     		idPer = String.valueOf(per.getId());
			out.print("<option value='"+idPer+"'>"+nomPer+"</option>");
			}
		%>
        </select>
         &nbsp;
         <input type="submit" name="Submit" value="+"  onClick="window.open('regPersona.jsp','mywindow','width=700,height=300,toolbar=yes, location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes, resizable=yes')" /></td>
        <td colspan="3" rowspan="3"><textarea name="comentario" id="comentario" cols="40" rows="5"></textarea></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>Local</td>
        <td colspan="2" nowrap="nowrap"><select name="idlocal" id="idlocal">
		<%
			conAnalista cl = new conAnalista();
				ArrayList locales= cl.getLocales();
				String nomLoc;
				String idLoc;
				String dirLoc;
				analista loc;
			out.print("<option value='-1'> -- </option>");
				
				     		for (int i = 0; i < locales.size(); i++) {
				     		loc=(analista) locales.get(i); 
				     		idLoc=String.valueOf(loc.getId());
				     		nomLoc=loc.getNombre();
				     		dirLoc= loc.getDireccion();
				     		
				     		idLoc = String.valueOf(loc.getId());
			out.print("<option value='"+idLoc+"'>"+nomLoc+" / "+dirLoc+"</option>");
			}
		%>
                </select>
          &nbsp;
          <input type="submit" name="Submit2" value="+" onClick="window.open('regLocal.jsp','mywindow','width=700,height=200,toolbar=no, location=no,directories=no,status=no,menubar=no,scrollbars=no, resizable=no')" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td nowrap="nowrap">Monto ent. </td>
        <td colspan="2"><input type="text" name="mnentre" id="mnentre" onkeyup="calculaCuotas();calculaTotal();calculaUtilidad();" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td nowrap="nowrap">Num Cuotas</td>
        <td colspan="2"><input name="cuotas" id="cuotas" type="text" size="10" maxlength="10" onkeyup="calculaCuotas();calculaTotal();calculaUtilidad();" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>Interes</td>
        <td width="17%"><input name="interes" id="interes" type="text" size="10" maxlength="10"  onkeyup="calculaCuotas();calculaTotal();calculaUtilidad();"/>
          %</td>
        <td colspan="5" rowspan="7" nowrap="nowrap" align="left">
		<iframe src="http://www.google.com" width="500" height="165" align="left">		</iframe>		</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>	
      <tr>
        <td>&nbsp;</td>
        <td>Cuotas</td>
        <td><input name="mncuota" id="mncuota" type="text" size="10" maxlength="10" onclick="calculaCuotas()"/></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>Monto tot. </td>
        <td><input name="mntotal" id="mntotal" type="text" size="10" maxlength="10" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>Utilidad</td>
        <td><input name="utilidad" id="utilidad" type="text" size="10" maxlength="10" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><a href="#" onclick="setPago();">Agregar pago </a></td>
        <td><input name="montopago" id="montopago" type="text" size="15" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
	</form>
	</td>
  </tr>
  
  <tr>
  <td><input type="button" name="btnGuardar" id="btnGuardar"  value="Grabar" onclick="setPrestamo()" />
  <input type="button" name="btnCancelar" id="btnCancelar" value="Cancelar" onclick="document.getElementById('btnGuardar').value='Grabar';limpiar();" /></td>
 
  </tr>
 
  <tr>
    <td><table width="100%" border="0">
      <tr>
        <td colspan="10" bgcolor="#BEDFEF">Busqueda</td>
        </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        
        
        
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr> 
  <tr>
    <td>
	<iframe id="frRepPrestamos" name="frRepPrestamos" width="1000" height="400"> </iframe>
	</td>
  </tr>
</table>

</body>
<%

try
{
String idpersona = request.getParameter("idpersona");



if (idpersona.length()>=1)
out.print("<script>");
out.print("document.getElementById('idpersona').value="+idpersona+";");
out.print("repPrestamos("+idpersona+");");

out.print("</script>");
}
catch(Exception e)
{
}
//if (document.getElementById("idpersona").value=='-1')


 %>


</html>